package org.vectrics.skill.struts;

import org.vectrics.common.util.DateSync;
import org.vectrics.evaluation.EvaluationType;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.apache.log4j.Category;
import javax.servlet.jsp.JspException;
import org.vectrics.skill.UserEvaluation;
import org.vectrics.skill.struts.UserEvaluationForm;
import org.vectrics.struts.VectricsStrutsAction;
import org.vectrics.struts.VectricsActionData;
import org.vectrics.user.Person;
import org.vectrics.user.PersonEncounterStatusType;


public class SaveEvaluationAction extends VectricsStrutsAction {
    private static Category log = Category.getInstance(SaveEvaluationAction.class);
    public static String ACTION_NAME = "save_detail_skills";
    
    public String doExecute(VectricsActionData actionData)	throws JspException {
    	String forwardCode = "success";
    	log.debug("execute() called");
    	if (actionData.wasButtonPressed("cancel") == true) {
    		log.debug("Cancel button pressed.");
        	forwardCode = "cancel";
    	} else {
    		log.debug("Cancel button NOT pressed.");
	    	if (actionData.getForm() == null) {
	    		log.error("execute() - form not passed (is null)");
	    		throw new JspException("execute() - form not passed (is null)");
	    	}
    		UserEvaluationForm theForm = (UserEvaluationForm)actionData.getForm();
    		String evaluationId = actionData.getRequest().getParameter("evaluationId");
    		UserEvaluation userEval = null;
    		PersonEncounterStatusType encounterStatus = RecruitServiceLocator.getPersonService().findPersonEncounterStatusType(PersonEncounterStatusType.STATUS_SCHEDULED);
    		
    		Long idType = new Long(theForm.getType());
    		EvaluationType type =  RecruitServiceLocator.getSkillProvider().getTypeEvaluation(idType);
    		
    		if (evaluationId == null) {
    			log.info("evaluationId was not passed as request parameter, assuming this is a new evaluation");
    			userEval = RecruitServiceLocator.getSkillProvider().createEvaluation(
    			        CoreServiceLocator.getPersonService().findPersonById(new Long(theForm.getUserId())), 
    			        CoreServiceLocator.getPersonService().findPersonById(new Long(theForm.getEvaluatorId())),
    			        encounterStatus, type , new Long(theForm.getScore()));
    		}
    		else {
    			log.info("existing evaluation being saved with new properties");
    			userEval = RecruitServiceLocator.getSkillProvider().findEvaluationById(new Long(evaluationId));
    			userEval.setDateCreated(DateSync.getCurrentTime());
    			userEval.setDateStart(theForm.getDatePerformed());
    		}
    		userEval.setDescription(theForm.getDescription());
    		userEval.setNotes(theForm.getNotes());
    		userEval.setDescription(theForm.getDescription());
    		userEval.setScore(new Long(theForm.getScore()));
    		
    		userEval.setType(type);
    		
    		Person evaluator = CoreServiceLocator.getPersonService().findPersonById(new Long(theForm.getEvaluatorId()));
    		userEval.setLeadPerson(evaluator);
    		RecruitServiceLocator.getSkillProvider().updateEvaluation(userEval);
    		
    		theForm.setId(userEval.getId().toString());
    		actionData.getRequest().setAttribute("evaluationId", userEval.getId().toString());
    		actionData.setRequestForm(theForm);
    	}
    	
        return (forwardCode);
    }


}